package tools;

public class LogonReplyPacket extends Packet{

	private String reply;
	private String name;

	public LogonReplyPacket() {}
	public LogonReplyPacket(String replay, String name) {
		super();
		this.reply = replay;
		this.name = name;
	}

	public String getReply() {
		return reply;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setReply(String replay) {
		this.reply = replay;
	}

	public byte[] serialize() {
		int totleLength = 12+reply.getBytes().length+name.getBytes().length;

		System.out.println(totleLength);

		byte[] buffer = new byte[totleLength];

		int offset = 0;

		byte totleBuffer[] = Util.int2bytes(totleLength);
		System.arraycopy(totleBuffer, 0, buffer, offset, 4);//把数组totlebuffer 复制到buffer中  长度为4  totleBuffer从0开始 buffer 从offset开始

		offset += 4;
		byte typeBuffer[] = Util.int2bytes(Service.PACKET_TYPE.S2C_LOGON);
		System.arraycopy(typeBuffer, 0, buffer, offset, 4);

		offset += 4;
		byte replayLengthBuffer[] = Util.int2bytes(reply.getBytes().length);
		System.arraycopy(replayLengthBuffer, 0, buffer, offset, 4);

		offset += 4;
		System.arraycopy(reply.getBytes(), 0, buffer, offset, reply.getBytes().length);

		offset += reply.getBytes().length;
		System.arraycopy(name.getBytes(), 0, buffer, offset, name.getBytes().length);

		return buffer;

	}

	@Override
	public void unserialize(byte[] data) {

		System.out.println(data.length);
		// decode total length
		int totalLength = Util.bytes2int(data, 0);

		// decode user name length
		int userNameLength = Util.bytes2int(data, 8);

		reply = new String(data, 12, userNameLength);

		name = new String(data, 12 + userNameLength, totalLength - 12 - userNameLength);

//		int totleLength = Util.bytes2int(data, 0);
//		System.out.println(totleLength);
//
//		int offset = 8;
//		int replayLength = Util.bytes2int(data, offset);
//
//		offset += 4;
//		reply = new String(data,offset,replayLength);
//
//		offset += replayLength;
//		name = new String(data,offset,totleLength-offset);



	}




}
